package com.yc.goods.biz;

import com.yc.entity.GoodsType;
import com.yc.entity.Goodsinfo;
import com.yc.goods.dao.GoodsInfoDao;

import com.yc.goods.dao.GoodsTypeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsInfoBizImpl implements GoodsInfoBiz {
    @Autowired
    private GoodsInfoDao goodsInfoDao;
    @Autowired
    private GoodsTypeBiz goodsTypeBiz;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Goodsinfo> findAll() {
        List<Goodsinfo> list = this.goodsInfoDao.selectList(null);
        return list;
    }
    /**
     * 通过商品名进行查询
     * @param gname
     * @return
     */
    @Override
    public List<Goodsinfo> findByGname(String gname) {
        List<Goodsinfo> list = goodsInfoDao.findByGname(gname);
        return list;
    }
    /**
     * 通过商品编号集合进行查询
     * @param gnos
     * @return
     */
    @Override
    public List<Goodsinfo> findByGno(List<Integer> gnos) {
        List<Goodsinfo> list = goodsInfoDao.findByGno(gnos);
        System.out.println(list);
        return list;
    }

    /**
     * 页面显示前四条数据
     * @return
     */
    @Override
    public Map<String, Object> findbyTnameTop() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Goodsinfo> list = goodsInfoDao.finds();
            for (int i = 0; i < list.size(); i++) {
                String[] arr = list.get(i).getPics().split(";");
                if(null != arr && arr.length > 0){
                    list.get(i).setPics(arr[0]);
                }
            }
            List<GoodsType> types = goodsTypeBiz.findAll();
            map.put("goods", list);
            map.put("types", types);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Goodsinfo findByGno(Integer gno)     {
        Goodsinfo goodsinfo = null;
        try {
            goodsinfo= goodsInfoDao.selectById(gno);
            String pics = (String) goodsinfo.getPics();
            String[] pic = pics.split(";");
            goodsinfo.setPics(pic[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return goodsinfo;
    }


}
